package typingballoon;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author personal
 */
public class SplashScreen extends GameCanvas implements Runnable {

    private TypingBalloonMIDlet midlet;
    private AnimatedSprite spBalloon;
    private static int FRAME_BALLOON_WIDTH = 0;
    private static int FRAME_BALLOON_HEIGHT = 0;
    private LayerManager lmgr;
    private String pathBalloon = "";
    private String pathTitle = "";
    private Image imgTitle;
    private boolean running = false;
    private int loop = 0;    
    Thread thread = null;
    //xy
    private int xShadow = 0;
    private int yShadow = 0;
    private int xTitle = 0;
    private int yTitle = 0;
    private int xString = 0;
    private int yString = 0;
    //speed
    private int speedGame = 50;
    
    
    //disable and paused
    //boolean
    private boolean isDisablePortrait = false;
    private boolean isPaused = false;
    //button
    private ButtonImage btnResume;
    //image
    private Image imgBtnResume;
    private Image imgDisablePortrait;
    private int acuanDisable = 320;
    private String pathBtnResume = "";
    private String pathDisable = "/disableportrait.png";

    public SplashScreen(TypingBalloonMIDlet midlet) {
        super(true);
        this.midlet = midlet;
        setFullScreenMode(true);

        if (getWidth() == 640) {
            FRAME_BALLOON_WIDTH = 94;
            FRAME_BALLOON_HEIGHT = 100;
            pathBalloon = "/balloon01_10_640.png";
            pathTitle = "/title640.png";
            pathBtnResume = "/btnResume640.png";
        } else if (getWidth() > acuanDisable) {
            FRAME_BALLOON_WIDTH = 94;
            FRAME_BALLOON_HEIGHT = 100;
            pathBalloon = "/balloon01_10_640.png";
            pathTitle = "/title640.png";
            pathBtnResume = "/btnResume640.png";
            isDisablePortrait = true;
            isPaused = true;
        } else {
            FRAME_BALLOON_WIDTH = 47;
            FRAME_BALLOON_HEIGHT = 50;
            pathBalloon = "/balloon01_10_320.png";
            pathTitle = "/title320.png";
            pathBtnResume = "/btnResume320.png";
        }

        try {
            imgTitle = Image.createImage(pathTitle);
            imgBtnResume = Image.createImage(pathBtnResume);
            imgDisablePortrait = Image.createImage(pathDisable);

            int xyArea[] = new int[4];

            int xResume = getWidth() - imgBtnResume.getWidth();
            int yResume = getHeight() - imgBtnResume.getHeight();
            xyArea[0] = xResume; //x1
            xyArea[1] = xResume + imgBtnResume.getWidth(); //x2
            xyArea[2] = yResume; //y1
            xyArea[3] = yResume + imgBtnResume.getHeight(); //y2
            btnResume = new ButtonImage("resume", imgBtnResume, imgBtnResume,
                    imgBtnResume, xResume,
                    yResume, xyArea, Graphics.TOP | Graphics.LEFT);

            //animated sprite
            spBalloon = new AnimatedSprite(Image.createImage(pathBalloon),
                    FRAME_BALLOON_WIDTH, FRAME_BALLOON_HEIGHT);
            spBalloon.defineReferencePixel(FRAME_BALLOON_WIDTH / 2, FRAME_BALLOON_HEIGHT / 2);

            lmgr = new LayerManager();
            lmgr.append(spBalloon);
        } catch (IOException ex) {
        }
        //new Thread(this).start();
        //setFullScreenMode(true);

        if (getWidth() < getHeight()) {
            if (getWidth() > acuanDisable) {
                isDisablePortrait = true;
                //System.out.println("PAUSED OY!");
                paused();
            } else {
                isDisablePortrait = false;
            }
        }
        updatePositions();
    }

    public void start() {
        if (!isPaused) {
            running = true;
            thread = new Thread(this);
            thread.start();
        }
    }

    public void run() {
        Graphics g = getGraphics();
        while (running) {
            drawDisplay(g);
            if (loop > 99) {
                running = false;
            }
            try {
                Thread.sleep(speedGame);
            } catch (InterruptedException ie) {
            }
        }

        if (loop > 99) {
            if (getWidth() < getHeight()) {
                if (getHeight() > acuanDisable) {
                } else {
                    //midlet.showMenuCanvas(false);
                    //midlet.showTypingCanvas640();
                    midlet.showMenuCanvas();
                    /*
                    if (getWidth() > acuanDisable) {
                    midlet.showTypingCanvas640();
                    } else {
                    midlet.showTypingCanvas();
                    }
                     * 
                     */
                    
                }
            } else {
                //midlet.showMenuCanvas(false);
                //midlet.showTypingCanvas640();
                midlet.showMenuCanvas();
                /*
                if (getWidth() > acuanDisable) {
                midlet.showTypingCanvas640();
                } else {
                midlet.showTypingCanvas();
                }
                 * 
                 */
            }
        }

    }

    protected void drawDisplay(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());

        //spBalloon.nextFrame();
        if (spBalloon.getFrame() == (spBalloon.getFrameSequenceLength() - 1)) {
            //spBalloon.setFrame(0);
            spBalloon.nextFrame();
            spBalloon.setVisible(false);
            //System.out.println("oi");            
        } else {
            spBalloon.nextFrame();
            //System.out.println("AH!!");
        }
        //System.out.println(spBalloon.getFrameSequenceLength());
        lmgr.paint(g, 0, 0);
        loop++;
        g.drawImage(imgTitle, xTitle,
                yTitle, Graphics.TOP | Graphics.LEFT);

        g.setColor(0x0000ff);
        yString = spBalloon.getY() + (spBalloon.getHeight() / 4);
        g.drawString(String.valueOf(loop) + " %", xString, yString,
                Graphics.TOP | Graphics.LEFT);
        //paint disini
        //sampai sini        

        //flush off-screen buffer to display
        flushGraphics();
    }

    /*
    private void dismiss() {
    if (isShown()) {
    host.showMenuCanvas();
    }
    }
     * 
     */
    protected void sizeChanged(int w, int h) {
        //super.sizeChanged(w, h);
        if (getWidth() < getHeight()) {
            if (getWidth() > acuanDisable) {
                isDisablePortrait = true;
                //System.out.println("PAUSED OY!");
                paused();
            } else {
                isDisablePortrait = false;
            }
        } else {
            isDisablePortrait = false;
            updatePositions();
            Graphics g = getGraphics();
            drawDisplay(g);
            if (isPaused) {
                int xResume = getWidth() - imgBtnResume.getWidth();
                int yResume = getHeight() - imgBtnResume.getHeight();

                btnResume.setPositionXY(xResume, yResume,
                        Graphics.TOP | Graphics.LEFT);
                btnResume.paint(g);
            }
        }
        updatePositions();
    }

    private void updatePositions() {
        xTitle = getWidth() / 2 - (imgTitle.getWidth() / 2);
        yTitle = getHeight() / 2 - (imgTitle.getHeight());
        xString = getWidth() / 2;
        yString = spBalloon.getY() + (spBalloon.getHeight() / 4);
        xShadow = getWidth() / 2 - spBalloon.getWidth();
        yShadow = getHeight() / 2;
        spBalloon.setPosition(xShadow, yShadow);
    }

    protected void hideNotify() {        
        //super.hideNotify();
        //hideNotify();
        paused();
        int xResume = getWidth() - imgBtnResume.getWidth();
        int yResume = getHeight() - imgBtnResume.getHeight();

        btnResume.setPositionXY(xResume, yResume,
                Graphics.TOP | Graphics.LEFT);
    }

    protected void showNotify() {
        //super.showNotify();
        //showNotify();
        if (!isDisablePortrait) {
            if (isPaused) {
                Graphics g = getGraphics();
                btnResume.paint(g);
                //checkForKeys();
            }
        }
    }

    private void paused() {
        isPaused = true;
        running = false;
        if (isDisablePortrait) {
            Graphics g = getGraphics();
            drawDisablePortrait(g);
        }
    }

    private void drawDisablePortrait(Graphics g) {
        g.drawImage(imgDisablePortrait, 0, 0, Graphics.TOP | Graphics.LEFT);
        flushGraphics();
    }

    protected void pointerPressed(int x, int y) {
        if (isPaused) {
            if (x > btnResume.xImage
                    && x < (btnResume.xImage + btnResume.imageWidth)
                    && y > btnResume.yImage
                    && y < (btnResume.yImage + btnResume.imageHeight)) {
                isPaused = false;
                start();
                repaint();
            }
        }
    }
}
